package com.yeraphael.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeraphael.entity.EmpLog;
import com.yeraphael.entity.PageBeandf;
import com.yeraphael.entity.Student;
import com.yeraphael.entity.StudentQueryParam;
import com.yeraphael.mapper.EmpLogMapper;
import com.yeraphael.mapper.StudentMapper;
import com.yeraphael.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private EmpLogMapper empLogMapper;

    @Override
    public Boolean getStudentCountByClazzId(Integer id) {
//        boolean hasStudents = studentMapper.getStudentCountByClazzId(id) > 0;
        Integer count = studentMapper.getStudentCountByClazzId(id);
        return count != null && count > 0;
    }

    @Override
    public PageBeandf getStudents(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Student> studentList = studentMapper.getStudents(param);
        Page page = (Page) studentList;

        return new PageBeandf(page.getTotal(), page.getResult());
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short)0);
        student.setViolationScore((short)0);
        studentMapper.addStudent(student);
    }

    @Override
    public Student getStudent(Integer id) {
        Student student = studentMapper.getStudent(id);
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    @Override
    public void deleteStudent(List<Integer> ids) {
        studentMapper.deleteStudent(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void violation(Integer id, Short score) {
        Student student = null;
        try {
//        1.根据学员id查询该学生的信息
            student = studentMapper.getStudent(id);

            Short newScore = (short) (student.getViolationScore() + score);
            Short newCount = (short) (student.getViolationCount() + 1);
//        2.修改分数和次数信息
            student.setViolationScore(newScore);
            student.setViolationCount(newCount);

//        3.将修改信息保存update操作
            studentMapper.updateStudent(student);
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setInfo("修改学生的违纪信息 id:"+student.getId()+" 姓名:"+ student.getName() + " 违纪次数:"+student.getViolationCount()+" 违纪分数:"+student.getViolationScore());
            empLog.setOperateTime(LocalDateTime.now());
            empLogMapper.insert(empLog);
        }
    }



}
